package com.lanou.renrenxin.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.base.BaseApplication;

import java.util.List;

/**
 * Created by zy on 16/3/5.
 * 群聊界面
 */
public class GroupActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private LinearLayout creatGroup;
    private Button btnBack,btnCreat;
    private ListView listViewGroup;
    private List<EMGroup> groupList;
    private Handler handler;
    private GroupListAdapter groupListAdapter;
    private TextView tvDivider;
    //更新群组列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        //动态注册广播接收器刷新列表
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("action.refreshGroup");
        registerReceiver(refreshBroadCastReceiver,intentFilter);

        addview();
        setup();
    }


    private void addview() {
        creatGroup = (LinearLayout) findViewById(R.id.creatgroup_ll);
        btnBack = (Button) findViewById(R.id.btnBack);
        listViewGroup = (ListView) findViewById(R.id.lv_group);
        tvDivider = (TextView) findViewById(R.id.group_divide_tv);
        btnCreat = (Button) findViewById(R.id.btnCreat);
    }


    public void setup() {
        creatGroup.setOnClickListener(this);
        btnBack.setOnClickListener(this);
        btnBack.setVisibility(View.VISIBLE);
        btnCreat.setVisibility(View.GONE);
        //获取群组列表
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what){
                    case 10:
                   groupList = (List<EMGroup>) msg.obj;
                    if (groupList!=null){
                       tvDivider.setVisibility(View.VISIBLE);
                    groupListAdapter = new GroupListAdapter(BaseApplication.getContext());
                    groupListAdapter.addData(groupList);
                    listViewGroup.setAdapter(groupListAdapter);
                    }
                    break;
                }

                return false;
            }
        });


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    groupList = EMGroupManager.getInstance().getGroupsFromServer();
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
                Message msg = new Message();
                msg.what = 10;
                msg.obj = groupList;
                handler.sendMessage(msg);
            }
        }).start();

        listViewGroup.setOnItemClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.creatgroup_ll:
                Intent intentNewGroup = new Intent(this, NewGroupActivity.class);
                startActivity(intentNewGroup);
                break;
            case R.id.btnBack:
                finish();
                break;
        }
    }

    //群聊list点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    //群组列表适配器
    private class GroupListAdapter extends BaseAdapter {
        private Context context;
        private List<EMGroup> groupList;

        public GroupListAdapter(Context context) {
            this.context = context;
        }

        public void addData(List<EMGroup> data) {
            groupList = data;
        }

        @Override
        public int getCount() {
            return groupList.size() > 0 && groupList != null ? groupList.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return groupList.size()>0 && groupList!= null?groupList.get(position):null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.item_group,null);
                holder = new ViewHolder();
                holder.textView = (TextView) convertView.findViewById(R.id.item_group_tv);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (groupList!= null){
                holder.textView.setText(groupList.get(position).getGroupName());
            }
            convertView.setBackgroundResource(R.color.colorWrite);
            return convertView;
        }

        public class ViewHolder {
            private TextView textView;
        }
    }
    //广播接收器,刷新群组list
    private BroadcastReceiver refreshBroadCastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("action.refreshGroup")){
                setup();
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁广播
        unregisterReceiver(refreshBroadCastReceiver);
    }
}
